﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Windows.Forms;
using ERP.BusinessEntities.Base;
using ERP.BusinessEntities.Factory;
using System.Security.Policy;
using ERP.BusinessEntities.Util;

namespace Core.Base
{
    public class BaseEntity
    {
        public BaseModule module;

        public int iMainObjectID = -1;
        public BaseBusinessInfo _mainObject;
        public BaseBusinessInfo MainObject
        {
            set
            {
                _oldMainObject = _mainObject; 
                if (value != null)
                    _mainObject = value;
                else
                    _mainObject = (BaseBusinessInfo)BusinessFactory.CreateBusinessObject(strMainTable + "Info");

                if (dicModuleObjects.ContainsKey(strMainTable))
                    dicModuleObjects[strMainTable] = _mainObject;

                if (_mainObject != null)
                {
                    object objMainID = DynamicObject.ReflectUtil.GetProperty(_mainObject, "ID");
                    if (objMainID != null)
                        iMainObjectID = Convert.ToInt32(objMainID);
                    else
                        iMainObjectID = -1;
                }else
                    iMainObjectID = -1;


                InvalidateObject(MainObject);

            }
            get
            {
                return _mainObject;
            }
        }

        public BaseBusinessInfo _oldMainObject;

        public String strMainTable = String.Empty;
        public BaseBusinessController MainCtrl;

        public Dictionary<String, BaseBusinessInfo> dicModuleObjects = new Dictionary<string, BaseBusinessInfo>();
        public Dictionary<String, BindingSource> dicBindingSources = new Dictionary<string, BindingSource>();
        public Dictionary<String, ISGVList> dicLists = new Dictionary<string, ISGVList>();

        public virtual void Init()
        {
            InitModuleObjects();
        }

        /// <summary>
        /// Init and register Main Object, Module Objects
        /// </summary>
        public virtual void InitModuleObjects()
        {

        }

        public virtual void InitLists()
        {
        }

        public void ReloadMainObject(Object obj)
        {
            Object objMainID = AppContext.GetProperty(obj, "ID");
            if (objMainID == null) return;

            int iMainObjID = Convert.ToInt32(objMainID);

            iMainObjectID = iMainObjID;

            Object MainObj = MainCtrl.GetObjectById(iMainObjectID);
            if (MainObj != null)
            {
                MainObject = (BaseBusinessInfo)MainObj;
            }else
                MainObject = (BaseBusinessInfo)obj;

        }

        public void AddModuleObject(String strModuleObjectName, BaseBusinessInfo obj)
        {
            if (dicModuleObjects.ContainsKey(strModuleObjectName) == false)
            {
                dicModuleObjects.Add(strModuleObjectName, obj);
            }
        }

        public void AddMainObject(String _strMainTable, BaseBusinessInfo obj)
        {
            strMainTable = _strMainTable;
            MainCtrl = (BaseBusinessController)BusinessFactory.CreateBusinessCtrl(strMainTable + "Controller");
            if (dicModuleObjects.ContainsKey(strMainTable) == false)
            {
                dicModuleObjects.Add(strMainTable, obj);
            }

            MainObject = obj;
        }


        public virtual void InvalidateObject(Object obj)
        {
            foreach (KeyValuePair<String, ISGVList> pair in dicLists)
            {
                pair.Value.InvalidateObject(obj);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj">the object make invalidate</param>
        public virtual void InvalidateModuleObject(EntityObject obj)
        {

        }


        #region Save
        public virtual void ActionSave(ActionType action)
        {

            if (action == ActionType.Edit)
            {
                AppContext.SetProperty(MainObject, AppContext.cstCreatedUser, AppContext.CurrentUserInfo.No);
                AppContext.SetProperty(MainObject, AppContext.cstCreatedDate, DateTime.Now);
                AppContext.SetProperty(MainObject, AppContext.cstUpdatedUser, AppContext.CurrentUserInfo.No);
                AppContext.SetProperty(MainObject, AppContext.cstUpdatedDate, DateTime.Now);
                MainCtrl.Update(MainObject);

            }
            else
            {
                AppContext.SetProperty(MainObject, AppContext.cstUpdatedUser, AppContext.CurrentUserInfo.No);
                AppContext.SetProperty(MainObject, AppContext.cstUpdatedDate, DateTime.Now);
                MainCtrl.CreateObject(MainObject);
            }

            BeforeSaveList();
            SaveLists();
            AfterSaveList();
        }

        public virtual void ActionDelete()
        {
            DeleteLists();
            MainCtrl.Delete(MainObject);
        }

        public virtual void BeforeSaveList()
        {
        }

        public virtual void SaveLists()
        {
            if (String.IsNullOrEmpty(strMainTable)) return;
            //if (dicLists.ContainsKey(strMainTable) == false) return;

            foreach (KeyValuePair<String, ISGVList> pair in dicLists)
            {
                pair.Value.SaveList(true);
            }
        }

        public virtual void AfterSaveList()
        {
        }

        public virtual void DeleteLists()
        {
            if (String.IsNullOrEmpty(strMainTable)) return;
            if (dicLists.ContainsKey(strMainTable) == false) return;

            foreach (KeyValuePair<String, ISGVList> pair in dicLists)
            {
                pair.Value.SaveList(true);
            }
        }

        #endregion


    }
}
